package core.C3P0.datasource;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;



public class C3P0DataSource 
{
	private static ComboPooledDataSource datasource=null;
	private static Connection con;
	
	static
	{
		try 
		{
			datasource = new ComboPooledDataSource("OrderSystem");
			con = datasource.getConnection();
		} 
		catch (SQLException e) 
		{
			System.out.println(e);
		}
	}
	
	
	
	public static Connection GetConnection() throws SQLException
	{
		if(con==null)
		{
			con=datasource.getConnection();
		}
		return con;
	}
	
	public  static void ReleaveConnection() throws SQLException
	{
		if(con!=null)
			con.close();
	}
	
	public  static void main(String args[]) throws Exception
	{
		/*-------------直接使用实例化数据库连接池数据源方法来创建，此方法不推荐-------------*/
	/*	ComboPooledDataSource cpds = new ComboPooledDataSource(); 
		cpds.setDriverClass( "com.microsoft.sqlserver.jdbc.SQLServerDriver" ); //loads the jdbc driver 
		cpds.setJdbcUrl( "jdbc:sqlserver://localhost:1433;DatabaseName=OrderSystem" ); 
		cpds.setUser("sa");
		cpds.setPassword("123"); 
		
		System.out.println(cpds.getConnection());  */
		/*-------------直接使用实例化数据库连接池数据源方法来创建，此方法不推荐-------------*/
		
		
		
		/*-------推荐使用读取xml文件的方法------------*/
		//ComboPooledDataSource cpds = new ComboPooledDataSource("OrderSystem");//读取name-config为OrderSystem
		//System.out.println(cpds.getConnection());
		//System.out.println(con);
		/*-------推荐使用读取xml文件的方法------------*/
	}
}
